//
//  HRewardHeaderView.swift
//  StarTimesON
//
//  Created by Toj on 7/18/23.
//  Copyright © 2023 StarTimes. All rights reserved.
//

import UIKit

protocol HRewardHeaderViewDelegate: NSObjectProtocol {
    func rewardHeaderViewDidTapGetVIP(_ headerView: HRewardHeaderView)
}

extension HRewardHeaderViewDelegate {
    func rewardHeaderViewDidTapGetVIP(_ headerView: HRewardHeaderView) { }
}

class HRewardHeaderView: UIView {
    
    weak var delegate: HRewardHeaderViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupMakeAddSubviews()
        setupMakeInitialize()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupMakeLayoutSubviews()
    }
    
    /* V装饰 */
    private lazy var decoratorImageView: UIImageView = {
        let view = UIImageView()
        view.image = "player_product_v_icon".to_image
        return view
    }()
    
    /* 标题 */
    private lazy var titleLabel: UILabel = {
        let view = UILabel()
        view.textAlignment = .center
        view.text = "VIP est nécessaire pour une visualisation complète"
        view.textColor = "#5E3A1E".to_color
        view.font = 14.to_systemFont
        return view
    }()
    
    /* 空白内容 */
    private lazy var contentView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.cm_cornerRadius = 8
        return view
    }()
    
    /* free标签 */
    private lazy var freeLabel: UILabel = {
        let view = UILabel()
        view.text = "GRATUITO"
        view.textColor = "#5E3A1E".to_color
        view.font = 8.to_boldFont
        return view
    }()
    
    /* free背景图 */
    private lazy var freeImageView: UIImageView = {
        let view = UIImageView()
        view.image = "player_product_free_bgimg".to_image
        view.st_corner = 5
        return view
    }()
    
    /* free标题 */
    private lazy var freeTitleLabel: UILabel = {
        let view = UILabel()
        view.text = "Obter VIP gratuitamente"
        view.textColor = "#5E3A1E".to_color
        view.font = 18.to_boldFont
        return view
    }()
    
    /* free子标题 */
    private lazy var freeSubtitleLabel: UILabel = {
        let view = UILabel()
        view.text = "Regardez une publicité et obtenez des longueurs VIP au hasard"
        view.textColor = "#A67128".to_color
        view.font = 10.to_systemFont
        view.numberOfLines = 2
        return view
    }()
    
    /* 看广告渐变 */
    private lazy var getVIPImageView: UIImageView = {
        let view = UIImageView()
        view.image = "player_product_getVIP_bgimg".to_image
        view.cm_cornerRadius = 12
        return view
    }()
    
    /* 看广告文案提示 */
    private let kWatchAdTitle = "Obtenir VIP"
    private lazy var getVIPLabel: UILabel = {
        let view = UILabel()
        view.textColor = "#5E3A1E".to_color
        view.font = 13.to_boldFont
        return view
    }()
    
    /* 看广告按钮 */
    private lazy var getVIPButton: UIButton = {
        let view = UIButton()
        view.addTarget(self, action: #selector(didTapGetVIP), for: .touchUpInside)
        return view
    }()
    
    /* 自动播放View */
    private lazy var autoPlayView: UIButton = {
        let view = UIButton()
        view.titleLabel?.font = 7.to_systemFont
        view.setTitle("Lecture automatique à chaque fois", for: .normal)
        view.setTitleColor("#929292".to_color, for: .normal)
        view.setImage("player_productAd_autoPlay_nor".to_image, for: .normal)
        view.setImage("player_productAd_autoPlay_sel".to_image, for: .selected)
        return view
    }()
    
    /* 自动播放可点击范围 */
    private lazy var autoPlayButton: UIButton = {
        let view = UIButton()
        view.addTarget(self, action: #selector(didTapAutoPlay), for: .touchUpInside)
        return view
    }()
    
    /* 倒计时 */
    private let kWatchAdTotalTime = 3
    private var timer: Timer?
    private var remainingTime = 0
    
    deinit {
        print("-->shantao: class:\(cm_objectName), func: \(#function)")
    }
}

// MARK: - 点击事件/ 通知事件回调
@objc private extension HRewardHeaderView {
    
    func didTapAutoPlay() {
        autoPlayView.isSelected = !autoPlayView.isSelected
        STDiskManager.manager.setCache(key: .rewardAdAutoPlay, value: autoPlayView.isSelected)
        
        guard autoPlayView.isSelected else {
            stopTimer()
            return
        }
        setWatchText(number: kWatchAdTotalTime)
        startTimer()
    }
    
    func didTapGetVIP() {
        stopTimer()
        
        delegate?.rewardHeaderViewDidTapGetVIP(self)
    }
    
    func countDownCallBack() {
        remainingTime -= 1
       
        setWatchText(number: remainingTime)
        
        guard remainingTime <= 0 else { return }
        didTapGetVIP()
    }
}

// MARK: - Public
extension HRewardHeaderView {
    
    func show(admobModel: STAdmobModel? = nil) {
        guard admobModel != nil else { return }
        
        setWatchText(number: kWatchAdTotalTime)
        
        guard autoPlayView.isSelected else { return }
        
        startTimer()
    }
}

// MARK: - Private
private extension HRewardHeaderView {
    
    func setWatchText(number: Int = 0) {
        var text = kWatchAdTitle
        if number > 0 && autoPlayView.isSelected {
            text += "(\(number))"
        }
        getVIPLabel.text = text
    }
    
    func startTimer() {
        stopTimer()
        
        remainingTime = kWatchAdTotalTime
        timer = Timer.st_scheduledTimer(timeInterval: 1, target: self, selector: #selector(countDownCallBack))
    }
    
    func stopTimer() {
        timer?.invalidate()
        timer = nil
        setWatchText()
    }
}

// MARK: - 初始化设置
private extension HRewardHeaderView {
    
    func setupMakeAddSubviews() {
        
        /* V装饰 */
        addSubview(decoratorImageView)
        
        /* 标题 */
        addSubview(titleLabel)
        
        /* 空白内容 */
        addSubview(contentView)
        
        /* free背景图 */
        addSubview(freeImageView)
        
        /* free标签 */
        addSubview(freeLabel)
        
        /* free标题 */
        addSubview(freeTitleLabel)
        
        /* free子标题 */
        addSubview(freeSubtitleLabel)
        
        /* 看广告渐变 */
        addSubview(getVIPImageView)
        
        /* 看广告文案提示 */
        addSubview(getVIPLabel)
        
        /* 看广告按钮 */
        addSubview(getVIPButton)
        
        /* 自动播放View */
        addSubview(autoPlayView)
        
        /* 自动播放点击 */
        addSubview(autoPlayButton)
    }
    
    func setupMakeLayoutSubviews() {
        /* V装饰 */
        decoratorImageView.snp.makeConstraints { make in
            make.left.top.bottom.equalTo(self)
            make.width.equalTo(133)
        }
        
        /* 标题 */
        titleLabel.snp.makeConstraints { make in
            make.left.right.equalTo(self)
            make.top.equalTo(12)
        }
        
        /* 空白内容 */
        contentView.snp.makeConstraints { make in
            make.bottom.equalTo(self).inset(14)
            make.height.equalTo(70)
            make.left.right.equalTo(self).inset(16)
        }
        /* free标签 */
        let freeWidth = freeLabel.cm_stringOneLineWidth
        freeLabel.snp.makeConstraints { make in
            make.left.equalTo(contentView).offset(20)
            make.top.equalTo(contentView).offset(18)
            make.height.equalTo(12)
            make.width.equalTo(freeWidth)
        }
        
        /* free背景图 */
        freeImageView.snp.makeConstraints { make in
            make.left.equalTo(freeLabel.snp.left).offset(-5)
            make.right.equalTo(freeLabel.snp.right).offset(5)
            make.height.centerY.equalTo(freeLabel)
        }
        
        /* 看广告按钮 */
        getVIPImageView.snp.makeConstraints { make in
            make.right.equalTo(contentView.snp.right).offset(-16)
            make.centerY.equalTo(contentView).offset(-4)
            make.width.equalTo(94)
            make.height.equalTo(24)
        }
        /* 看广告文案提示 */
        getVIPLabel.snp.makeConstraints { make in
            make.center.equalTo(getVIPButton)
        }
        
        getVIPButton.snp.makeConstraints { make in
            make.edges.equalTo(getVIPImageView)
        }
        
        /* 自动播放View */
        autoPlayView.snp.makeConstraints { make in
            make.left.right.equalTo(getVIPButton)
            make.top.equalTo(getVIPButton.snp.bottom).offset(4)
            make.height.equalTo(8)
        }
        
        /* 自动播放点击 */
        autoPlayButton.snp.makeConstraints { make in
            make.left.right.equalTo(autoPlayView)
            make.top.equalTo(getVIPButton.snp.bottom)
            make.bottom.equalTo(contentView)
        }
        
        /* free标题 */
        freeTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(freeImageView.snp.right).offset(4)
            make.centerY.equalTo(freeImageView)
            make.right.equalTo(autoPlayButton.snp.left).offset(-4)
        }
        
        /* free子标题 */
        freeSubtitleLabel.snp.makeConstraints { make in
            make.left.equalTo(freeImageView)
            make.top.equalTo(freeTitleLabel.snp.bottom).offset(2)
            make.right.equalTo(freeTitleLabel)
        }
    }
    
    func setupMakeInitialize() {
        autoPlayView.isSelected = STDiskManager.manager.cacheBool(key: .rewardAdAutoPlay)
    }
}
